package com.google.code.bankagregator.adapter.bank.forum.parser.credit;

import com.google.code.bankagregator.adapter.exception.AdapterConnectException;
import com.google.code.bankagregator.entities.*;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: kirill
 * Date: 15.06.13
 * Time: 1:46
 * To change this template use File | Settings | File Templates.
 */
public class ForumAutoCreditParser {
    private static final int INDEX_P_WITH_PREPAYMENT = 2;
    private static final int INDEX_P_WITH_MAX_AMOUNT = 4;
    private static final int INDEX_P_WITH_TERM = 3;
    private static final int INDEX_DIV = 1;
    private static final String CREDIT_URL = "http://www.forum.ua/forumua/ua/mainnavigation/retail_1/lending_3/" +
            "carloans_1/carloans_1.jsp";
    private static final String SELECT_PARAM = "div[class=section clearfix]";
    public Document con = null;
    private Bank bank = null;
    private Credit credit;

    public ForumAutoCreditParser() throws AdapterConnectException {
        bank = new Bank();
        bank.setName("ПАТ \"БАНК ФОРУМ\"");
        bank.setUrl("http://www.forum.ua/forumua/ua/mainnavigation/home/home.html");
        try {
            con = Jsoup.connect(CREDIT_URL).timeout(5000).get();
        } catch (IOException e) {
            throw new AdapterConnectException(e.getMessage());
        }
        credit = new Credit();
    }

    /**
     * Get credit`s params
     *
     * @return credit`s params
     * @throws AdapterConnectException
     */
    public List<Credit> getCredit() throws AdapterConnectException {
        List<Credit> creditParam = new ForumAutoCreditParser().parser();

        return creditParam;
    }

    /**
     * @return credit`s params
     */
    public List<Credit> parser() {
        List<Credit> creditParam = new ArrayList<>();
        Element prepaymentEl = con.select(SELECT_PARAM).get(INDEX_DIV).
                select("p").get(INDEX_P_WITH_PREPAYMENT);
        String prepaymentStr = prepaymentEl.text().replaceAll("[^0-9.]+", "");

        Element termEl = con.select(SELECT_PARAM).get(INDEX_DIV).
                select("p").get(INDEX_P_WITH_TERM);
        String termStr = termEl.text().replaceAll("[^0-9.]+", "");
        Element maxAmountEl = con.select(SELECT_PARAM).get(INDEX_DIV).
                select("p").get(INDEX_P_WITH_MAX_AMOUNT);
        String maxAmountStr = maxAmountEl.text().replaceAll("[^0-9.]+", "").replace(" ", "").replace(".", "");

        int maxAmount = Integer.parseInt(maxAmountStr);
        int term = Integer.parseInt(termStr) * 365;
        double prepayment = Double.parseDouble(prepaymentStr);

        credit.setPrepayment(prepayment);
        credit.setMaxAmount(maxAmount);
        credit.setTerm(term);
        credit.setCurrency(Currency.UAH);
        credit.setType(CreditType.AUTO);
        credit.setBank(bank);
        credit.setUrl(CREDIT_URL);
        credit.setState(State.ACTIVE);
        if (credit.getType().name().equals("AUTO")) {
            credit.setName("Кредит на покупку авто");
        } else if (credit.getType().name().equals("REALTY")) {
            credit.setName("Кредит на покупку недвижимости");
        }
        creditParam.add(credit);
        return creditParam;
    }
}

